////////Liquid_level_sensor//////////
/*-----------------------------------------------------------------------------------------------
read_attr_num=51, uploadNum=46, totalFrame=2
{"head":{"gatewayId":"gw_1C3283005078","replaceId":"","msgId":610,"timeStamp":1736221290,"response":"readAttr"},"data":
{"nodeType":1,"channel":2,"privAddr":1,"mac":"FFFFFFFFFFFF","name":"oilLevel_2_1","pid":"4001","totalFrame":2,"currFrame":1,"totalAttr":46,"itemCnt":40},
"attrLst":[{"name":"olH","lable":"液位高","class":17,"dpid":32768,"val":282},{"name":"olL","lable":"液位低","class":17,"dpid":32769,"val":19222},
{"name":"tempe","lable":"温度1","class":17,"dpid":32770,"val":298},{"name":"alarmEn","lable":"液位报警使能","class":20,"dpid":1537,"val":0},
{"name":"thOlL","lable":"液位过低阈值","class":20,"dpid":1540,"val":0},{"name":"thOlH","lable":"液位过高阈值","class":20,"dpid":1541,"val":0},
{"name":"thTL","lable":"温度过低阈值","class":20,"dpid":1158,"val":0},{"name":"thTH","lable":"温度过高阈值","class":20,"dpid":1159,"val":0},
{"name":"oilChkInterval","lable":"油耗检查周期","class":20,"dpid":1160,"val":0},{"name":"thOilConsume","lable":"油耗异常阈值","class":20,"dpid":1161,"val":0},
{"name":"tempeChkInterval","lable":"温升检查周期","class":20,"dpid":1162,"val":0},{"name":"thTempeRise","lable":"温升异常阈值","class":20,"dpid":1163,"val":0},
{"name":"olLastChk","lable":"上次检查液位值","class":48,"dpid":65025,"val":28202},{"name":"tempeLastChk","lable":"上次温升检查值","class":48,"dpid":65027,"val":0},
{"name":"olLastChkTime","lable":"上次油位检查时间","class":48,"dpid":65024,"val":1736221290},
{"name":"tempeLastChkTime","lable":"上次温升检查时间","class":48,"dpid":65026,"val":0},
{"name":"oilConsumeAlmRec","lable":"油耗异常报警记录时间","class":176,"dpid":65028,"val":1736221290},
{"name":"tempeRiseAlmRec","lable":"温升异常报警记录时间","class":176,"dpid":65029,"val":0},
{"name":"t","lable":"温度计算值","class":17,"dpid":32772,"val":18.625},
{"name":"rptT","lable":"温度上报值","class":16,"dpid":48,"val":1863},
{"name":"ol","lable":"液位计算值","class":17,"dpid":32771,"val":282.01922607421875},
{"name":"rptOl","lable":"液位上报值","class":16,"dpid":1281,"val":28202},
{"name":"almTL","lable":"低温报警","class":17,"dpid":65280,"val":0},
{"name":"almTH","lable":"高温报警","class":17,"dpid":65281,"val":1},{"name":"almOlL","lable":"液位低报警","class":17,"dpid":65282,"val":0},
{"name":"almOlH","lable":"液位高报警","class":17,"dpid":65283,"val":1},{"name":"currTime","lable":"当前检查时间","class":48,"dpid":65284,"val":1736221290},
{"name":"chkStart","lable":"开始油耗检查","class":48,"dpid":65285,"val":0},{"name":"chkTempeStart","lable":"开始温升检查","class":48,"dpid":65286,"val":1},
{"name":"oilChange","lable":"液位变动值","class":48,"dpid":65287,"val":0},{"name":"condOlFall","lable":"液位异常下降条件","class":48,"dpid":65288,"val":0},
{"name":"condOlRise","lable":"液位异常上升条件","class":48,"dpid":65289,"val":0},{"name":"chkTimeOk","lable":"检查时间有效","class":48,"dpid":65290,"val":1},
{"name":"chkOlOk","lable":"检查液位有效","class":48,"dpid":65291,"val":1},{"name":"almOlFall","lable":"液位异常下降报警","class":17,"dpid":65294,"val":0},
{"name":"almOlRise","lable":"液位异常上升报警","class":17,"dpid":65295,"val":0},{"name":"almOilConsume","lable":"油耗异常报警","class":17,"dpid":65296,"val":0},
{"name":"tempeRise","lable":"温度变化值","class":17,"dpid":65297,"val":1863},{"name":"tempeFallCond","lable":"温降异常条件","class":48,"dpid":65298,"val":0},
{"name":"tempeRiseCond","lable":"温升异常条件","class":48,"dpid":65299,"val":1}]}

-----------------------------------------------------------------------------------------*/

//#include "dev_liquid_level.h"


#ifndef __DEV_LIQUID_LEVEL_H__
#define __DEV_LIQUID_LEVEL_H__

#include "dev_module.h"
#include "iot_message.pb.h"
#include <stdint.h>
#include "iot_node.h"




#define  liquid_max_scale_0_01mm   (100000U)    //1000mm===1000x100 (0.01mm)
#define  liquid_max_dev_addr        (245U)      // 液位设备地址	R/W	否	配置参数	uint8_t	0~255
#define  liquid_max_channel          (31U)      // 液位设备通道编号	R/W	否	配置参数	uint8_t	0~31




// #define DEFAULT_PID              (0xC27A)               //默认pid
// #define DEFAULT_HARDWARE_STR     "2024-08-01 V1000"     //默认硬件版本号
// #define DEFAULT_PRODUCT_TYPE     "ml307r-sd-200"        //默认产品类型
// #define DEFAULT_MANUFACTURE_INFO "2024-09-29-p001"      //默认生产信息




/*-----------------------------------------------------------------------------------------------
0x0500	液位报警信息	RO	是	运行参数	uint32_t			0	"0=正常，1=告警
bit0：液位过低
bit1：液位过高
bit2：温度过低
bit3：温度过高
其余：保留"
0x0501	液位	RO	否	运行参数	uint32_t		0.01mm		
0x0502	温度	RO	否	运行参数	int16_t		0.01℃
									
									
0x0600	液位设备地址	R/W	否	配置参数	uint8_t	0~255		1	
0x0601	液位报警使能	R/W	否	配置参数	uint32_t			0	"0=不启用，1=启用

bit0：液位过低
bit1：液位过高
bit2：温度过低
bit3：温度过高
其余：保留"
0x0602	液位设备通道编号	R/W	否	配置参数	uint8_t	0~31	////?????		
									
									
									
0x0604	液位过低阈值	R/W	否	配置参数	uint32_t		0.01mm		
0x0605	液位过高阈值	R/W	否	配置参数	uint32_t		0.01mm		

//---ver1.32--debug----------
0x0606	,//1542	thTL	温度过低阈值	R/W	否	配置参数	int16_t	-32768~32767	0.01℃
0x0607	,//1543	thTH	温度过高阈值	R/W	否	配置参数	int16_t	-32768~32767	0.01℃
0x0608	,//1544	oilChkInterval	监测时间	R/W	否	配置参数	uint32_t		sec
0x0609	,//1545	thOilConsume	液位异常值	R/W	否	配置参数	uint32_t		0.01mm									
              
              
0x0801	软件版本	RO	否	软件版本	string	最大32字节			
0x0802	硬件版本	RO	否	硬件版本	string	最大32字节			
0x0803	产品型号	RO	否	产品型号	string	最大32字节			
0x0804	生产信息	RO	否	生产信息	string	最大32字节			生产日期，生产批次等信息
0x0805	厂家信息	RO	否		string	最大32字节			生产商名称信息

-----------------------------------------------------------------------------------------*/

/////-----设备类型----
//#define   DEV_LIQUID_TYPE     (0xD006)     //液位传感器（D006H） 




////以下枚举的取值 需要结合 数组 liquid_iot_id_table[liquid_index_end]的下标进行关联的定义---同步更新----
typedef enum{

    liquid_index_run_data_begin      = 0,    // 设备运行参数
    liquid_index_run_data_end     = 3,    
    liquid_index_config_data_begin   = 3,    // 设备配置参数-------------超过 8个时需要分帧处理
    liquid_index_factory_info_begin  = 12,   // 设备出厂信息
    liquid_index_end = 16+1,

} liquid_attr_index_e;





#pragma pack(1)

//0x0500	液位报警信息 "0=正常，1=告警
typedef union{
    struct{
        uint32_t under_level :  1;      // 液位过低
        uint32_t over_level  :  1;	    // 液位过高
        uint32_t under_temper : 1;      // 温度过低
        uint32_t over_temper  : 1;	    // 温度过高

        uint32_t  decrease_level:1;    //液位异常减少 increase decrease 
        uint32_t  increase_level:1;    //液位异常增多

        uint32_t res1:26;
    }bit;
    uint32_t value;
} liquid_alarm_u;


//液位传感器（D006H）的数据
///---------RO 数据--------//
typedef struct{

 //   int8_t    temper;      //温度  1℃   
    int16_t    temper;      //温度  0.01℃   

    uint32_t  level_mm;    //液位  0.01mm

    liquid_alarm_u  alarm;   //0x0500	液位报警信息"0=正常，1=告警
                             //bit0：液位过低bit1：液位过高bit2：温度过低bit3：温度过高

} liquid_data_t;


// 有效量程	1000mm
// 死区长度	上死区51mm，测杆死区40mm
// 测杆总长度	 1091mm+2mm


// 0x0606	1542	thTL	温度过低阈值	R/W	否	配置参数	int16_t	-32768~32767	0.01℃
// 0x0607	1543	thTH	温度过高阈值	R/W	否	配置参数	int16_t	-32768~32767	0.01℃
// 0x0608	1544	oilChkInterval	监测时间	R/W	否	配置参数	uint32_t		sec
// 0x0609	1545	thOilConsume	液位异常值	R/W	否	配置参数	uint32_t		0.01mm


///---------R/W	配置参数--------//
typedef struct{

    uint8_t   addr;              //液位设备地址  0~255			
    uint8_t   channel;           //0x0602	液位设备通道编号	0~31	
    int16_t   under_temper_th;   //温度过低阈值  1℃ 
    int16_t   over_temper_th;    //温度过高阈值  1℃   
    uint32_t  under_level_th;    //液位过低阈值  0.01mm
    uint32_t  over_level_th;     //液位过高阈值  0.01mm
    uint32_t  monitor_sec_th;    //监测时间		sec
    uint32_t  monitor_level_th;  //液位异常值	0.01mm


    liquid_alarm_u   alarm_enable; //液位报警使能 "0=不启用，1=启用

} liquid_config_t;




//---配置节点信息----------


typedef union
{
	struct{	
		
	  uint16_t online                  :1;	  //节点在线标志-----1:在线，0:离线，

	  uint16_t res                     :15;  
		
	}bit;
	
  uint16_t all;

} liquid_node_flags_u;




/*  ----------------------- --------------------------
typedef struct{

    char soft_ver[33];
    char hard_ver[33];
    char product_model[33];     //0x0803	产品型号

    char production_info[33];        //生产信息----生产日期，生产批次等信息
    char manufacturer[33];      //0x0805	厂家信息- 最大32字节 生产商名称信息----manufacturer--factory--producer---

} liquid_info_t;

----------------------- --------------------------*/

// typedef enum{

//     index_1,    //
//     index_max,

// } liquid_index_e;


//#define   LIQUID_NODE_NUM_MAX    (255U)  
#define   LIQUID_NODE_NUM_MAX    (NODE_NUM_MAX)     ///与配置节点总数相关-----



typedef struct{
    liquid_data_t      data;
    liquid_config_t    config;
 //   liquid_info_t      info;
    dev_attr_com_info_t  info;
    liquid_node_flags_u  flags;

 //   char   dev_name_str[32];   /////车工json协议需要的设备名称    
} liquid_attr_t;


#pragma pack()




extern const  uint16_t  liquid_iot_id_table[liquid_index_end];



/**
 * @brief 
 * 
 */
void liquid_param_init(void);

uint8_t liquid_add_obj(uint8_t idx,uint8_t channel,uint8_t addr);


/**
 * @brief 
 * 
 * @param in idx 
 * @return liquid_attr_t ==NULL 查询出错,   !NULL 查询ok
 */
liquid_attr_t*  liquid_get_obj(uint8_t idx) ;  //liquid_attr_get


/**
 * @brief liquid_set_obj() 赋值节点数据
 * 
 * @param in idx
 * @param in p_attr
 * @return 0=失败，1=成功 
 */
uint8_t  liquid_set_obj(uint8_t idx,liquid_attr_t* p_attr )  ;



  //  liquid_index_run_data_begin      = 0,    // 设备运行参数
  //  liquid_index_config_data_begin   = 3,    // 设备配置参数
/**
 * @brief 根据下标，获取iot_id 的编号
 * 
 * @param idx 
 */
uint16_t  liquid_get_iot_id(uint8_t idx);


/**
 * @brief 
 * 
 * @param attr_id 
 * @param attr_out 
 */
void liquid_get_attr(uint8_t idx,uint16_t attr_id, IotModel* attr_out);


/**
 * @brief 
 * 
 * @param attr_in 
 * @return uint8_t 
 */
uint8_t liquid_set_attr(uint8_t idx,IotModel* attr_in);


void liquid_test_tick_min(uint8_t idx);

////刷新节点运行数据
void liquid_update_tick_min(uint8_t idx,int8_t temper ,uint32_t liquid );

void liquid_obj_update(uint8_t idx,liquid_attr_t* pliq_in  );




#endif //------__DEV_LIQUID_LEVEL_H__




















